package com.bop.dehong.main;

import java.util.ArrayList;
import java.util.List;

import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

import com.base.dehong.widget.CustomViewPager;
import com.bop.dehong.R;
import com.bop.dehong.adapter.MyFragStatePagerAdapter;
import com.bop.dehong.base.BaseFragment;
import com.bop.dehong.base.BaseFragmentActivity;
/**
 * 继承此类的Activity包含的ContentView中，必须要有ViewPager,且Id一定要为viewPager
 * @author TangWenhan/YeFengyun
 */
public abstract class BottomBarActivity extends BaseFragmentActivity {
	private List<Fragment> mFragments;
	private List<View> mBottomBarViews = new ArrayList<View>();
	public CustomViewPager mViewPager;
	private MyFragStatePagerAdapter mAdapter;
	private boolean isFristStart = true;
	public int mPage = 0;
	int tempPosition = -1;
	onBottomBarListener mBarListener;
	
	public interface onBottomBarListener{
		public boolean nBottomBar(int position);
	}
	
	public void setBottomBarListener(onBottomBarListener bottomBarListener) {
		this.mBarListener = bottomBarListener;
	}

	
	public boolean isFristStart() {
		return isFristStart;
	}

	public void setFristStart(boolean isFristStart) {
		this.isFristStart = isFristStart;
	}

	@Override
	protected void initTop() {
		mPage = getIntent().getIntExtra("page", 0);
		initFragments();
	}

	@Override
	protected void initUI() {
		mViewPager = getView(R.id.viewPager);
		mFragments = getFragments();
		mBottomBarViews = getBottomViews();
	}

	@Override
	protected void initData() {
		mAdapter = new MyFragStatePagerAdapter(getSupportFragmentManager(), mFragments);
		mViewPager.setAdapter(mAdapter);
		mViewPager.setOnPageChangeListener(onPageChangeListener);
		initBottomBar();
		turnTargetView(mPage);
	}
	protected abstract List<Fragment> getFragments();
	
	protected abstract List<View> getBottomViews();
	
	protected abstract void initFragments();

	public void initBottomBar() {
		for (int i = 0; i < mBottomBarViews.size(); i++) {
			mBottomBarViews.get(i).setTag(i);
			mBottomBarViews.get(i).setOnClickListener(bottomBarListener);
		}
	}
	/**
	 * 跳转到目标界面
	 * 
	 * @param position
	 */
	protected void turnTargetView(int position) {
		turnTargetView(position,true);
	}
	
	protected void turnTargetView(int position,boolean d) {
		selectBottomBar(position);
		mViewPager.setCurrentItem(position,d);
	}
	
	/**
	 * 控制下面的图片状态变换
	 * @param position
	 */
	private void selectBottomBar(int position){
		if (tempPosition == position) {
			return;
		}
		if (tempPosition >= 0) {
			mBottomBarViews.get(tempPosition).setSelected(false);
		}
		mBottomBarViews.get(position).setSelected(true);
		tempPosition = position;
	}
	
	OnPageChangeListener onPageChangeListener = new OnPageChangeListener() {
		
		@Override
		public void onPageSelected(int i) {
			selectBottomBar(i);
			if (mBarListener != null) {
				if (mBarListener.nBottomBar(i)) {
//					return;
				}
			}
			BaseFragment bf = (BaseFragment) mFragments.get(i);
			bf.initData();
		}
		
		@Override
		public void onPageScrolled(int i, float f, int j) {
		}
		
		@Override
		public void onPageScrollStateChanged(int i) {
			// TODO Auto-generated method stub
		}
	};
	View.OnClickListener bottomBarListener = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
				int tag = (Integer) v.getTag();
				if (mBarListener != null) {
					if (mBarListener.nBottomBar(tag)) {
						return;
					}
				}
				if (tag < mFragments.size()) {
					turnTargetView(tag);
				}
			}
	};
	
	@Override
	public int getCourrentPosition() {
		return mViewPager.getCurrentItem();
	}
	public Fragment getCurrentFragment(){
		return mFragments.get(getCourrentPosition());
	}
	/**
	 * 退出应用,暂未做任何处理
	 */
	protected void exitApp() {
	}
}
